package com.qf.service.impl;



import com.qf.mapper.ProducterMapper;
import com.qf.mapper.SysUserMapper;
import com.qf.model.SysUser;
import com.qf.model.TProducter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ProducterMapper producterMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //校验用户名
        SysUser userByUsername = sysUserMapper.findUserByUsername(username);
        if (userByUsername==null){
            TProducter producter = producterMapper.findByName(username);
            if (producter == null ){
                System.out.println("用户名或者密码错误");
                return null;
            }
            //授权
            List<GrantedAuthority> list = new ArrayList<>();
            return new org.springframework.security.core.userdetails.User(username,producter.getPassword(),list);

        }
        //授权
        List<GrantedAuthority> list = new ArrayList<>();
        return new org.springframework.security.core.userdetails.User(username,userByUsername.getPassword(),list);





    }
}
